<?php

namespace System\library;

use System\library\safety\Safety;

/**
 * <b>Class</b> :        Pagination<br>
 * <b>Fonction</b> :     Generer une pagination de liens
 * <br>
 * @version              1.0
 * @author               Julien RAMEL
 * @version              2.0<br>
 *                       Possibilité de configurer le $_GET à chercher pour la pagination<br>
 *                       Debug des attributs<br>
 *                       Debug de la generation des liens preview<br>
 *                       Debug de l'url, on spécifie avant les $_GET qui pouvaient exister, puis on ajoute notre $_GET de pagination<br>
 *                       Debug du calcul du nombre de preview et next pages<br>
 *                       Mise en forme du retour dans un attribut séparer pour les boutons (previous & next) ainsi que le nombre de page
 * <br>
 * @author               Dread <dreadlokeur@gmail.com>
 * @version              3.0<br>
 *                       Refonte complète de la classe
 * <br>
 * @author               Dread <dreadlokeur@gmail.com>
 * @license              Licence publique générale GNU http://www.gnu.org/licenses/gpl.html
 * @copyright            Copyright 2011 - MidichloriansPHP and contributors
 * @package              System
 * @subpackage           library
 */
class Paginator {

    private $_name_get_page;
    private $_nb_total;
    private $_nb_per_page;
    private $_nb_prev;
    private $_nb_next;
    private $_view_prev;
    private $_view_next;
    private $_current_page;
    private $_minid;
    private $_nb_pages;
    private $_nb_final_prev;
    private $_nb_final_next;
    private $_nb_pages_total;
    private $_list_pages;
    private $_insert_page;
    private $_previous_button;
    private $_page_link = array();
    private $_is_current_page;
    private $_next_button;

    public function __construct($name_get_page, $nb_total, $nb_per_page, $nb_prev = 3, $nb_next = 3, $view_prev = true, $view_next = true) {
        //Attribution des valeurs
        $this->_name_get_page = $name_get_page;
        $this->_nb_total = $nb_total;
        $this->_nb_per_page = $nb_per_page;
        $this->_nb_prev = $nb_prev;
        $this->_nb_next = $nb_next;
        $this->_view_prev = $view_prev;
        $this->_view_next = $view_next;
        //Génération de l'url en cours...
        global $_GET;
        $this->_url = '?';
        foreach ($_GET as $k => $v) {
            if ($k != $this->_name_get_page)
                $this->_url .= $k . '=' . $v . '&';
        }
        //On construit la pagination
        $this->_constructPagination();
    }

    private function _constructPagination() {
        $this->_detectCurrentPage();
        $this->_countPages();
        $this->_calculFinalsPage();
        $this->_constructPages();
        $this->_setPreviousButton();
        $this->_determinePagesNumber();
        $this->_setNextButton();
    }

    private function _detectCurrentPage() {
        $this->_current_page = (isset($_GET['' . $this->_name_get_page . ''])) ? Safety::safetyInput($_GET['' . $this->_name_get_page . '']) : 1;
        $this->_minid = ($this->_current_page - 1) * $this->_nb_per_page;
    }

    private function _countPages() {
        $this->_nb_pages = ceil($this->_nb_total / $this->_nb_per_page);
    }

    private function _calculFinalsPage() {
        $this->_nb_final_prev = ($this->_current_page <= $this->_nb_prev) ? $this->_current_page - 1 : $this->_nb_prev;
        $this->_nb_final_next = ($this->_current_page >= ($this->_nb_pages - $this->_nb_next)) ? $this->_nb_pages - $this->_current_page : $this->_nb_next;

        if ($this->_nb_final_next > $this->_nb_next)
            $this->_nb_final_prev = $this->_nb_prev + ($this->_nb_next - $this->_nb_final_next);

        if ($this->_nb_final_prev > $this->_nb_prev)
            $this->_nb_final_next = $this->_nb_prev + ($this->_nb_prev - $this->_nb_final_prev);
    }

    private function _constructPages() {
        $this->_list_pages = array($this->_current_page);
        for ($i = 0; $i < $this->_nb_final_prev; $i++) {
            $this->_insert_page = ($this->_current_page - $i - 1);
            array_unshift($this->_list_pages, $this->_insert_page);
        }
        for ($i = 0; $i < $this->_nb_final_next; $i++) {
            $this->_insert_page = ($this->_current_page + $i + 1);
            array_push($this->_list_pages, $this->_insert_page);
        }
    }

    private function _setPreviousButton() {
        if ($this->_view_prev && $this->_current_page > 1)
            $this->_previous_button = $this->_url . $this->_name_get_page . '=' . ($this->_current_page - 1);
    }

    private function _determinePagesNumber() {
        foreach ($this->_list_pages as $page) {
            $this->_is_current_page = ($page == $this->_current_page) ? true : false;
            $this->_page_link[] = array(
                'page_link' => $this->_url . $this->_name_get_page . '=' . $page,
                'page_number' => $page,
                'is_current_page' => $this->_is_current_page);
        }
    }

    private function _setNextButton() {
        if ($this->_view_next && ($this->_current_page + 1) <= $this->_nb_pages)
            $this->_next_button = $this->_url . $this->_name_get_page . '=' . ($this->_current_page + 1);
    }

    public function getPreviousButton() {
        return $this->_previous_button;
    }

    public function getNextButton() {
        return $this->_next_button;
    }

    public function getPages() {
        return $this->_page_link;
    }
}

?>